package com.qeng.aiwanba.activity;

import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qeng.aiwanba.R;
import com.qeng.aiwanba.fragment.BaseFragment;
import com.qeng.aiwanba.util.ActivityManagerUtil;
import com.qeng.aiwanba.util.Constants;
import com.qeng.aiwanba.util.MyBridgeUtil;
import com.qeng.aiwanba.util.Util;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private TextView tab_main_tv;
    private TextView tab_bbs_tv;
    private TextView tab_user_tv;
//    private RelativeLayout welcome_rl;

    private BaseFragment mainFragment;
    private BaseFragment bbsFragment;
    private BaseFragment userFragment;

    private final String fragment_tag_main = "fragment_tab_main";
    private final String fragment_tag_bbs = "fragment_tab_bbs";
    private final String fragment_tag_user = "fragment_tab_user";

    private int currentTabIndex = 0;
    private BaseFragment currentFragment;

    private MyBroadcast myBroadcast;
    private IntentFilter intentFilter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        VersionTools.checkVersion(this,true);

        ActivityManagerUtil.popActivity(getLocalClassName(),this);

        initViews();

        registerBroadcast();

    }

    private void initViews(){
        tab_main_tv = (TextView) findViewById(R.id.tab_main);
        tab_bbs_tv = (TextView) findViewById(R.id.tab_bbs);
        tab_user_tv = (TextView) findViewById(R.id.tab_user);
//        welcome_rl = (RelativeLayout) findViewById(welcome_rl);

        tab_main_tv.setOnClickListener(this);
        tab_user_tv.setOnClickListener(this);
        tab_bbs_tv.setOnClickListener(this);

        Util.setStateShadow(this,(LinearLayout) findViewById(R.id.staus_ll));

        setTabContent(2);
        setTabContent(1);
        setTabContent(0);
    }

    private void registerBroadcast(){
        myBroadcast = new MyBroadcast();
        intentFilter = new IntentFilter(Constants.BROADCAST_NOTIFICATION_CLICK_DEAL);
        registerReceiver(myBroadcast,intentFilter);
    }

    @Override
    protected void onRestart() {
        currentFragment.noticeJsPageAppear();
        super.onRestart();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tab_main:
                if(currentTabIndex != 0) {
                    currentTabIndex = 0;
                    selectTab();
                }
                break;
            case R.id.tab_bbs:
                if(currentTabIndex != 1) {
                    currentTabIndex = 1;
                    selectTab();
                }
                break;
            case R.id.tab_user:
                if(currentTabIndex != 2) {
                    currentTabIndex = 2;
                    selectTab();
                }
                break;
        }
    }

    private void selectTab(){
        currentFragment.noticeMainTab(currentTabIndex);
        setTabContent(currentTabIndex);
        initTabRes(currentTabIndex);
    }

    public void setTabContent(int index) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        switch (index) {
            case 0:
                if (mainFragment == null) {
                    mainFragment = BaseFragment.newInstance("/index.html");
                    transaction.add(R.id.main_content, mainFragment, fragment_tag_main);
                }else{
                    transaction.hide(bbsFragment).hide(userFragment).show(mainFragment);
                }
                currentFragment = mainFragment;
                break;
            case 1:
                if (bbsFragment == null) {
                    bbsFragment = BaseFragment.newInstance("/usergame.html");
                    transaction.add(R.id.main_content, bbsFragment, fragment_tag_bbs);
                }else{
                    transaction.hide(mainFragment).hide(userFragment).show(bbsFragment);
                }
                currentFragment = bbsFragment;
                break;
            case 2:
                if (userFragment == null) {
                    userFragment = BaseFragment.newInstance("/user.html");
                    transaction.add(R.id.main_content, userFragment, fragment_tag_user);
                }else{
                    transaction.hide(mainFragment).hide(bbsFragment).show(userFragment);
                }
                currentFragment = userFragment;
                break;
        }
        Constants.topFrgment = currentFragment;
        transaction.commit();
    }

    private void initTabRes(int pos){
        tab_main_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_main_default,0,0);
        tab_bbs_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_game_default,0,0);
        tab_user_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_user_default,0,0);

        tab_bbs_tv.setTextColor(getResources().getColor(R.color.grey_text_unseletor));
        tab_main_tv.setTextColor(getResources().getColor(R.color.grey_text_unseletor));
        tab_user_tv.setTextColor(getResources().getColor(R.color.grey_text_unseletor));

        if(pos==0){
            tab_main_tv.setTextColor(getResources().getColor(R.color.grey_text_seletor));
            tab_main_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_main,0,0);
        }else if(pos==1){
            tab_bbs_tv.setTextColor(getResources().getColor(R.color.grey_text_seletor));
            tab_bbs_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_game,0,0);
        }else{
            tab_user_tv.setTextColor(getResources().getColor(R.color.grey_text_seletor));
            tab_user_tv.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.tab_user,0,0);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            currentFragment.noticeJsKeyBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        ActivityManagerUtil.pushActivity(getLocalClassName());
        super.onDestroy();
    }

    private class MyBroadcast extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Constants.BROADCAST_NOTIFICATION_CLICK_DEAL)){
                try {
                    JSONObject object = new JSONObject(intent.getStringExtra("message"));
                    String tabIndex = object.optString("TabIndex");
                    String htmlPath = object.optString("HtmlPath");
                    try {
                        currentTabIndex = Integer.valueOf(tabIndex);
                        selectTab();
                        MyBridgeUtil.getInstance().startFragment(MainActivity.this,htmlPath);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
